package com.jz.netthings.application.providerCost.service.impl;

import com.baomidou.mybatisplus.enums.SqlLike;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jz.netthings.appengine.baseData.ProviderInfo;
import com.jz.netthings.appengine.mapper.ProviderInfoMapper;
import com.jz.netthings.application.providerCost.bean.ProviderCost;
import com.jz.netthings.application.providerCost.bean.QueryProviderCost;
import com.jz.netthings.application.providerCost.mapper.mysql.ProviderCostMapper;
import com.jz.netthings.application.providerCost.service.ProviderCostService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jz.netthings.bean.system.JyCode;
import com.jz.netthings.bean.system.JyUser;
import com.jz.netthings.exception.AdscmSystemException;
import com.jz.netthings.security.utils.Token;
import com.jz.netthings.system.mapper.mysql.JyCodeMapper;
import com.jz.netthings.system.mapper.mysql.JyUserMapper;
import com.jz.netthings.utils.AdscmBaseUtils;
import com.jz.netthings.utils.AdscmDateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zyl
 * @since 2019-03-18
 */
@Service
public class ProviderCostServiceImpl extends ServiceImpl<ProviderCostMapper, ProviderCost> implements ProviderCostService {
    @Autowired
    JyUserMapper  jyUserMapper;
    @Autowired
    ProviderInfoMapper providerInfoMapper;
    @Autowired
    JyCodeMapper jyCodeMapper;
    @Value("${ly.upload.baseUrl}")
    String  photoUrl;

    @Override
    public Page<ProviderCost> selectProviderCost(Page<ProviderCost> page, QueryProviderCost queryProviderCost) throws AdscmSystemException {
        Wrapper<ProviderCost>  wrapper=new EntityWrapper<>();
        if (AdscmBaseUtils.getInstance().isNotEmpty(queryProviderCost.getProivderNo(),null)){
            wrapper.eq("providerNo",queryProviderCost.getProivderNo());
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(queryProviderCost.getRegisterNo(),null)){
            wrapper.like("registerNo",queryProviderCost.getRegisterNo(), SqlLike.RIGHT);
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(queryProviderCost.getStartCreateTime(),null)){
            wrapper.ge("createTime",queryProviderCost.getStartCreateTime());
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(queryProviderCost.getEndCreateTime(),null)){
            wrapper.le("createTime",queryProviderCost.getEndCreateTime());
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(queryProviderCost.getState(),null)){
            wrapper.eq("state",queryProviderCost.getState());
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(queryProviderCost.getIssueType(),null)){
            wrapper.eq("issueType",queryProviderCost.getIssueType());
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(queryProviderCost.getRzfs(),null)){
            wrapper.eq("rzfs",queryProviderCost.getRzfs());
        }
        page = super.selectPage(page, wrapper);
        List<ProviderCost> providerCosts = page.getRecords();
        for (ProviderCost providerCost : providerCosts) {
            if (AdscmBaseUtils.getInstance().isNotEmpty(providerCost.getProviderNo(),null)){
                ProviderInfo providerInfo = providerInfoMapper.selectById(providerCost.getProviderNo());
                if (AdscmBaseUtils.getInstance().isNotEmpty(providerInfo,null)){
                    providerCost.setProviderNoName(providerInfo.getName());
                    providerCost.setProviderNoPhoto(photoUrl+providerInfo.getPhoto());
                    providerCost.setCardId(providerInfo.getCardId());
                    providerCost.setIdNo(providerInfo.getIdNo());
                    providerCost.setAddress(providerInfo.getAddress());
                    providerCost.setPhone(providerInfo.getPhone());
                    providerCost.setAge(providerInfo.getAge().toString());
                    JyCode jyCode =new JyCode();
                    jyCode.setType(7);
                    jyCode.setCode(providerInfo.getSex());
                    jyCode= jyCodeMapper.selectOne(jyCode);
                    if (AdscmBaseUtils.getInstance().isNotEmpty(jyCode,null)){
                        providerCost.setSex(jyCode.getName());
                    }

                }else {
                    providerCost.setProviderNoName(providerCost.getProviderNo());
                }
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(providerCost.getUploader(),null)){
                JyUser jyUser = jyUserMapper.selectById(providerCost.getUploader());
                if (AdscmBaseUtils.getInstance().isNotEmpty(jyUser,null)){
                    providerCost.setUploaderName(jyUser.getName());
                }else {
                    providerCost.setUploaderName(providerCost.getUploader());
                }
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(providerCost.getPutWorker(),null)){
                JyUser jyUser = jyUserMapper.selectById(providerCost.getPutWorker());
                if (AdscmBaseUtils.getInstance().isNotEmpty(jyUser,null)){
                    providerCost.setPutWorkerName(jyUser.getName());
                }else {
                    providerCost.setPutWorkerName(providerCost.getPutWorker());
                }
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(providerCost.getUploadReceipt(),null)){
                providerCost.setUploadReceipt(photoUrl+providerCost.getUploadReceipt());
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(providerCost.getPhoto(),null)){
                providerCost.setPhoto(photoUrl+providerCost.getPhoto());
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(providerCost.getBaseMoney(),null)){
                JyCode jyCode =new JyCode();
                jyCode.setType(22);
                jyCode.setCode( String.valueOf(providerCost.getBaseMoney().intValue()));
                jyCode= jyCodeMapper.selectOne(jyCode);
                if (AdscmBaseUtils.getInstance().isNotEmpty(jyCode,null)){
                    providerCost.setBaseMoneyName(jyCode.getName());
                }else {
                    providerCost.setBaseMoneyName(providerCost.getBaseMoney().toString());
                }
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(providerCost.getRzfs(),null)){
                JyCode jyCode =new JyCode();
                jyCode.setType(6);
                jyCode.setCode( providerCost.getRzfs());
                jyCode= jyCodeMapper.selectOne(jyCode);
                if (AdscmBaseUtils.getInstance().isNotEmpty(jyCode,null)){
                    providerCost.setRzfsName(jyCode.getName());
                }else {
                    providerCost.setRzfsName(providerCost.getRzfs());
                }
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(providerCost.getState(),null)){
                JyCode jyCode =new JyCode();
                jyCode.setType(23);
                jyCode.setCode( providerCost.getState());
                jyCode= jyCodeMapper.selectOne(jyCode);
                if (AdscmBaseUtils.getInstance().isNotEmpty(jyCode,null)){
                    providerCost.setStateName(jyCode.getName());
                }else {
                    providerCost.setStateName(providerCost.getState());
                }
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(providerCost.getIssueType(),null)){
                JyCode jyCode =new JyCode();
                jyCode.setType(26);
                jyCode.setCode( providerCost.getIssueType());
                jyCode= jyCodeMapper.selectOne(jyCode);
                if (AdscmBaseUtils.getInstance().isNotEmpty(jyCode,null)){
                    providerCost.setIssueTypeName(jyCode.getName());
                }else {
                    providerCost.setIssueTypeName(providerCost.getIssueType());
                }
            }

        }
        page.setTotal(super.selectCount(wrapper));
        return page;
    }

    @Override
    public Boolean updateProviderCost(ProviderCost providerCost, Token token) throws AdscmSystemException {
    //查询出原来的记录
        ProviderCost oldproviderCost = super.selectById(providerCost.getId());
        if (AdscmBaseUtils.getInstance().isNotEmpty(providerCost.getUploadReceipt(),null)){
            String uploadReceipt = providerCost.getUploadReceipt().replace(photoUrl, "");
            if (AdscmBaseUtils.getInstance().isNotEmpty(oldproviderCost.getUploadReceipt(),null)){
                if (!oldproviderCost.getUploadReceipt().equals(uploadReceipt)){
                    oldproviderCost.setUploadReceipt(uploadReceipt);
                    oldproviderCost.setUploader(token.getUser().getId().toString());
                    oldproviderCost.setUploadReceiptTime(new Date());
                }
            }else {
                oldproviderCost.setUploadReceipt(uploadReceipt);
                oldproviderCost.setUploader(token.getUser().getId().toString());
                oldproviderCost.setUploadReceiptTime(new Date());
            }
        }

        if (AdscmBaseUtils.getInstance().isNotEmpty(providerCost.getState(),null)){
            //已处理人工发放
            if ("1".equals(providerCost.getState())){
                oldproviderCost.setPutOutDate(AdscmDateUtils.getFormateDate(new Date()));
                oldproviderCost.setPutOutStartTime(new Date());
                oldproviderCost.setPutOutEndTime(new Date());
                oldproviderCost.setEndTime(new Date());
                oldproviderCost.setIssuePlace(providerCost.getIssuePlace());
                oldproviderCost.setState("1");
                oldproviderCost.setRzfs("3");
                oldproviderCost.setIssueType("1");
                oldproviderCost.setPutWorker(token.getUser().getId().toString());
             }
        }
        oldproviderCost.setModifyTime(new Date());
        oldproviderCost.setSynDate(AdscmDateUtils.getFormateDate(new Date()));
        boolean b = super.updateById(oldproviderCost);
        return b;
    }
}
